1   package org.apache.lucene.queryparser.flexible.standard.config;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import java.util.HashMap;
21  import java.util.LinkedHashMap;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.TimeZone;
25  
26  import org.apache.lucene.analysis.Analyzer;
27  import org.apache.lucene.document.DateTools;
28  import org.apache.lucene.document.DateTools.Resolution;
29  import org.apache.lucene.queryparser.flexible.core.config.ConfigurationKey;
30  import org.apache.lucene.queryparser.flexible.core.config.FieldConfig;
31  import org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler;
32  import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;
33  import org.apache.lucene.queryparser.flexible.standard.processors.StandardQueryNodeProcessorPipeline;
34  import org.apache.lucene.search.MultiTermQuery;
35  import org.apache.lucene.search.MultiTermQuery.RewriteMethod;
36  
37  /**
38   * This query configuration handler is used for almost every processor defined
39   * in the {@link StandardQueryNodeProcessorPipeline} processor pipeline. It holds
40   * configuration methods that reproduce the configuration methods that could be set on the old
41   * lucene 2.4 QueryParser class.
42   * 
43   * @see StandardQueryNodeProcessorPipeline
44   */
45  public class StandardQueryConfigHandler extends QueryConfigHandler {
46  
47    /**
48     * Class holding keys for StandardQueryNodeProcessorPipeline options.
49     */
50    final public static class ConfigurationKeys  {
51      
52      /**
53       * Key used to set whether position increments is enabled
54       * 
55       * @see StandardQueryParser#setEnablePositionIncrements(boolean)
56       * @see StandardQueryParser#getEnablePositionIncrements()
57       */
58      final public static ConfigurationKey<Boolean> ENABLE_POSITION_INCREMENTS = ConfigurationKey.newInstance();
59      
60      /**
61       * Key used to set whether expanded terms should be lower-cased
62       * 
63       * @see StandardQueryParser#setLowercaseExpandedTerms(boolean)
64       * @see StandardQueryParser#getLowercaseExpandedTerms()
65       */
66      final public static ConfigurationKey<Boolean> LOWERCASE_EXPANDED_TERMS = ConfigurationKey.newInstance();
67  
68      /**
69       * Key used to set whether leading wildcards are supported
70       * 
71       * @see StandardQueryParser#setAllowLeadingWildcard(boolean)
72       * @see StandardQueryParser#getAllowLeadingWildcard()
73       */
74      final public static ConfigurationKey<Boolean> ALLOW_LEADING_WILDCARD = ConfigurationKey.newInstance();
75      
76      /**
77       * Key used to set the {@link Analyzer} used for terms found in the query
78       * 
79       * @see StandardQueryParser#setAnalyzer(Analyzer)
80       * @see StandardQueryParser#getAnalyzer()
81       */
82      final public static ConfigurationKey<Analyzer> ANALYZER = ConfigurationKey.newInstance();
83      
84      /**
85       * Key used to set the default boolean operator
86       * 
87       * @see StandardQueryParser#setDefaultOperator(org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler.Operator)
88       * @see StandardQueryParser#getDefaultOperator()
89       */
90      final public static ConfigurationKey<Operator> DEFAULT_OPERATOR = ConfigurationKey.newInstance();
91      
92      /**
93       * Key used to set the default phrase slop
94       * 
95       * @see StandardQueryParser#setPhraseSlop(int)
96       * @see StandardQueryParser#getPhraseSlop()
97       */
98      final public static ConfigurationKey<Integer> PHRASE_SLOP = ConfigurationKey.newInstance();
99      
100     /**
101      * Key used to set the {@link Locale} used when parsing the query
102      * 
103      * @see StandardQueryParser#setLocale(Locale)
104      * @see StandardQueryParser#getLocale()
105      */
106     final public static ConfigurationKey<Locale> LOCALE = ConfigurationKey.newInstance();
107     
108     final public static ConfigurationKey<TimeZone> TIMEZONE = ConfigurationKey.newInstance();
109     
110     /**
111      * Key used to set the {@link RewriteMethod} used when creating queries
112      * 
113      * @see StandardQueryParser#setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
114      * @see StandardQueryParser#getMultiTermRewriteMethod()
115      */
116     final public static ConfigurationKey<MultiTermQuery.RewriteMethod> MULTI_TERM_REWRITE_METHOD = ConfigurationKey.newInstance();
117 
118     /**
119      * Key used to set the fields a query should be expanded to when the field
120      * is <code>null</code>
121      * 
122      * @see StandardQueryParser#setMultiFields(CharSequence[])
123      * @see StandardQueryParser#getMultiFields()
124      */
125     final public static ConfigurationKey<CharSequence[]> MULTI_FIELDS = ConfigurationKey.newInstance();
126     
127     /**
128      * Key used to set a field to boost map that is used to set the boost for each field
129      * 
130      * @see StandardQueryParser#setFieldsBoost(Map)
131      * @see StandardQueryParser#getFieldsBoost()
132      */
133     final public static ConfigurationKey<Map<String,Float>> FIELD_BOOST_MAP = ConfigurationKey.newInstance();
134 
135     /**
136      * Key used to set a field to {@link Resolution} map that is used
137      * to normalize each date field value.
138      * 
139      * @see StandardQueryParser#setDateResolutionMap(Map)
140      * @see StandardQueryParser#getDateResolutionMap()
141      */
142     final public static ConfigurationKey<Map<CharSequence, DateTools.Resolution>> FIELD_DATE_RESOLUTION_MAP = ConfigurationKey.newInstance();
143     
144     /**
145      * Key used to set the {@link FuzzyConfig} used to create fuzzy queries.
146      * 
147      * @see StandardQueryParser#setFuzzyMinSim(float)
148      * @see StandardQueryParser#setFuzzyPrefixLength(int)
149      * @see StandardQueryParser#getFuzzyMinSim()
150      * @see StandardQueryParser#getFuzzyPrefixLength()
151      */
152     final public static ConfigurationKey<FuzzyConfig> FUZZY_CONFIG = ConfigurationKey.newInstance();
153     
154     /**
155      * Key used to set default {@link Resolution}.
156      * 
157      * @see StandardQueryParser#setDateResolution(org.apache.lucene.document.DateTools.Resolution)
158      * @see StandardQueryParser#getDateResolution()
159      */
160     final public static ConfigurationKey<DateTools.Resolution> DATE_RESOLUTION = ConfigurationKey.newInstance();
161     
162     /**
163      * Key used to set the boost value in {@link FieldConfig} objects.
164      * 
165      * @see StandardQueryParser#setFieldsBoost(Map)
166      * @see StandardQueryParser#getFieldsBoost()
167      */
168     final public static ConfigurationKey<Float> BOOST = ConfigurationKey.newInstance();
169     
170     /**
171      * Key used to set a field to its {@link NumericConfig}.
172      * 
173      * @see StandardQueryParser#setNumericConfigMap(Map)
174      * @see StandardQueryParser#getNumericConfigMap()
175      */
176     final public static ConfigurationKey<NumericConfig> NUMERIC_CONFIG = ConfigurationKey.newInstance();
177     
178     /**
179      * Key used to set the {@link NumericConfig} in {@link FieldConfig} for numeric fields.
180      * 
181      * @see StandardQueryParser#setNumericConfigMap(Map)
182      * @see StandardQueryParser#getNumericConfigMap()
183      */
184     final public static ConfigurationKey<Map<String,NumericConfig>> NUMERIC_CONFIG_MAP = ConfigurationKey.newInstance();
185     
186   }
187   
188   /**
189    * Boolean Operator: AND or OR
190    */
191   public static enum Operator {
192     AND, OR;
193   }
194 
195   public StandardQueryConfigHandler() {
196     // Add listener that will build the FieldConfig.
197     addFieldConfigListener(new FieldBoostMapFCListener(this));
198     addFieldConfigListener(new FieldDateResolutionFCListener(this));
199     addFieldConfigListener(new NumericFieldConfigListener(this));
200     
201     // Default Values
202     set(ConfigurationKeys.ALLOW_LEADING_WILDCARD, false); // default in 2.9
203     set(ConfigurationKeys.ANALYZER, null); //default value 2.4
204     set(ConfigurationKeys.DEFAULT_OPERATOR, Operator.OR);
205     set(ConfigurationKeys.PHRASE_SLOP, 0); //default value 2.4
206     set(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS, true); //default value 2.4
207     set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, false); //default value 2.4
208     set(ConfigurationKeys.FIELD_BOOST_MAP, new LinkedHashMap<String, Float>());
209     set(ConfigurationKeys.FUZZY_CONFIG, new FuzzyConfig());
210     set(ConfigurationKeys.LOCALE, Locale.getDefault());
211     set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD, MultiTermQuery.CONSTANT_SCORE_REWRITE);
212     set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, new HashMap<CharSequence, DateTools.Resolution>());
213     
214   }
215 
216 }